<template>
  <div>
    <el-card class="search-card">
      <BasicForm
        ref="formData"
        defaultExpandStatus
        :isScalingBtn="false"
        :formColumns="FORM_ITEMS"
        :dataSource="formData"
        :handleSearchForm="handleQueryForm"
        @getFormData="getFormData"
      />
    </el-card>
    <el-card class="table-card">
      <SkusTable
        ref="skusTable"
        showActionCol
        showPagination
        :actionColWidth="100"
        :tableHeight="490"
        :dataSource="tableInfo.list"
        :columns="tableInfo.columns"
        :loading="tableInfo.loading"
        :pageSizes="pageSizes"
        :pagination="pagination"
        :total="pagination.total"
        :actionRender="renderTableBtnAction"
        @pageChange="handlePageChange"
        @pageSizeSelect="handlePageSizeChange"
      />
    </el-card>
  </div>
</template>
<script>
import BasicForm from '@/components/BasicForm';
import SkusTable from '@/components/skusTable';
import { getTableAction } from '@/utils/getBtnsRender';
import { FORM_ITEMS, TABLE_COLUMNS } from './constants';
import skusPage from '@/mixins/commodity/skusPage';
import { getAuditListService } from '@/api/basics/unitManage/unitToAudit';
import { queryOAUrlService } from '@/api/public';
import { setAuthBtnId } from '@/utils/bizUtils';
import { getSearchParamBySearchType } from '@/utils/getUtils';

export default {
  mixins: [skusPage],
  components: {
    BasicForm,
    SkusTable,
  },
  data () {
    return {
      FORM_ITEMS,
      formData: {
        unitName: ''
      },
      tableInfo: {
        list: [],
        columns: TABLE_COLUMNS,
        loading: false,
      },
    }
  },
  mounted () {
    this.getTableData()
  },
  methods: {
    // 渲染表格操作栏按钮
    renderTableBtnAction (h, { row }) {
      const actionList = [
        { label: '审核', method: this.handleAudit, hasAuth: true }
      ];
      return getTableAction({ h, row, actionList, showBtn: this.showBtn });
    },
    // 获取表格数据 by 查询表单
    async getTableData () {
      setAuthBtnId('查询')
      const fixParam = [{ name: 'auditState', value: '1' }, { name: 'auditState_q', value: 'EQ' }]
      const data = {
        ...this.paginationParams,
        searchParam: JSON.stringify(getSearchParamBySearchType(FORM_ITEMS, this.formData).concat(fixParam))
      }
      const { retData } = await getAuditListService(data, state => { this.tableInfo.loading = state });
      if (retData) {
        const { results, totalNum } = retData;
        this.tableInfo.list = results;
        this.setTotalNum(totalNum)
      }
    },
    // 审核
    async handleAudit (row) {
      const res = await queryOAUrlService({ oaRequestId: row.workflowRequestId }, state => {})
      console.log(res)
      if (res) {
        window.open(res.retData)
      }
    }
  }
}
</script>
